package com.gaff.emp.core.modules.perm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.PageR;
import com.gaff.emp.core.modules.auth.JwtUtil;
import com.gaff.emp.core.modules.auth.LoginUserInfo;
import com.gaff.emp.core.modules.perm.controller.user.form.StatusForm;
import com.gaff.emp.core.modules.perm.controller.user.form.UpdateForm;
import com.gaff.emp.core.modules.perm.dao.SysDeptDao;
import com.gaff.emp.core.modules.perm.dao.SysRoleDao;
import com.gaff.emp.core.modules.perm.dao.SysUserDao;
import com.gaff.emp.core.modules.perm.entity.SysDept;
import com.gaff.emp.core.modules.perm.entity.SysRole;
import com.gaff.emp.core.modules.perm.entity.SysUser;
import com.gaff.emp.core.modules.perm.controller.user.form.CreateForm;
import com.gaff.emp.core.modules.perm.controller.user.form.PasswordForm;
import com.gaff.emp.core.modules.perm.service.SysUserService;
import com.gaff.emp.core.util.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysDeptDao sysDeptDao;

    @Override
    public SysUser queryByUserName(String username) {
        return sysUserDao.selectOne(Wrappers.<SysUser>query()
                .eq("username", username));
    }

    @Override
    public SysUser queryById(Integer id) {
        return sysUserDao.selectById(id);
    }

    @Override
    public List<SysUser> queryByRoleId(Integer roleId) {
        return sysUserDao.selectList(Wrappers.<SysUser>query()
                .eq("role_id", roleId));
    }

    @Override
    public void updatePassword(LoginUserInfo userInfo, PasswordForm form) {
        if (form.getPassword().equals(form.getNewPassword())) {
            throw new EmpException("原密码和新密码不能相同");
        }
        SysUser sysUser = sysUserDao.selectById(userInfo.getUser().getId());
        if (!sysUser.getPassword().equals(form.getPassword())) {
            throw new EmpException("原密码不正确");
        }
        sysUserDao.updatePasswordByUserId(userInfo.getUser().getId(), form.getNewPassword());
        JwtUtil.clearUserInfo(userInfo);
    }

    @Override
    public PageR queryUsersByPage(Map<String, Object> params) {
        String username = (String) params.get("username");
        IPage<SysUser> page = sysUserDao.selectPage(
                PageQuery.<SysUser>getPage(params),
                Wrappers.<SysUser>query()
                        .like(StrUtil.isNotBlank(username), "username", username));
        for (SysUser user : page.getRecords()) {
            user.setPassword("");
        }
        return new PageR(page);
    }

    @Override
    public void createUser(CreateForm form) {
        SysUser user = queryByUserName(form.getUsername());
        if (user != null) {
            throw new EmpException("用户名重复");
        }
        SysRole role = sysRoleDao.selectById(form.getRoleId());
        if (role == null) {
            throw new EmpException("角色不存在");
        }
        user = new SysUser();
        BeanUtil.copyProperties(form, user);
        if (form.getDeptId() != null) {
            SysDept dept = sysDeptDao.selectById(form.getDeptId());
            if (dept == null) {
                throw new EmpException("部门不存在");
            }
            user.setDeptName(dept.getName());
        }
        user.setRoleName(role.getRoleName());
        user.setPassword(SysUser.initPassword);
        user.setStatus(SysUser.normal);
        sysUserDao.insert(user);
    }

    @Override
    public void enableOrDisableUser(StatusForm form) {
        SysUser user = sysUserDao.selectById(form.getId());
        if (user == null) {
            throw new EmpException("用户不存在");
        }
        if (form.getStatus() != SysUser.locked && form.getStatus() != SysUser.normal) {
            throw new EmpException("用户状态错误");
        }
        user.setStatus(form.getStatus());
        sysUserDao.updateById(user);
    }

    @Override
    public void updateUser(UpdateForm form) {
        SysUser user = sysUserDao.selectById(form.getId());
        if (user == null) {
            throw new EmpException("用户不存在");
        }
        SysRole role = sysRoleDao.selectById(form.getRoleId());
        if (role == null) {
            throw new EmpException("指定的角色不存在");
        }
        user.setRoleName(role.getRoleName());
        if (form.getDeptId() != null) {
            SysDept dept = sysDeptDao.selectById(form.getDeptId());
            if (dept == null) {
                throw new EmpException("指定的部门不存在");
            }
            user.setDeptName(dept.getName());
        } else if (form.getDeptId() == null && user.getDeptId() != null) {
            user.setDeptName(null);
        }
        BeanUtil.copyProperties(form, user);
        sysUserDao.updateById(user);
    }

    @Override
    public void deleteById(Integer id) {
        sysUserDao.deleteById(id);
    }

}
